<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-10
 * Time: 16:28
 */
declare(strict_types=1);

namespace App\Collector;

use App\Annotation\AdminRequired;
use App\Annotation\GroupRequired;
use App\Annotation\LoginRequired;
use App\Annotation\PermissionMeta;
use App\Annotation\PermissionModule;
use App\Annotation\RefreshRequired;
use App\Lib\Enum\UserLevel;
use Hyperf\Di\Annotation\AnnotationCollector;

/**
 * Class PermissionMetaCollector
 * @package App\Collector
 */
class PermissionMetaCollector
{
    /**
     * 利用单例注入保存成员属性数据
     * @var array
     */
    private $metaMap = [];
    /**
     * 利用单例注入保存成员属性数据
     * @var array
     */
    private $userLevel = [];


    public function __construct()
    {
        $adminLevel = AnnotationCollector::getMethodsByAnnotation(AdminRequired::class);
        $groupLevel = AnnotationCollector::getMethodsByAnnotation(GroupRequired::class);
        $loginLevel = AnnotationCollector::getMethodsByAnnotation(LoginRequired::class);
        $refreshLevel = AnnotationCollector::getMethodsByAnnotation(RefreshRequired::class);
        $levels = array_merge($adminLevel,$groupLevel,$loginLevel,$refreshLevel);
        foreach ($levels as $item){
            $key = $item['class'].'#'.$item['method'];
            $this->userLevel[$key] = $item['annotation']->value;
        }
        $methodAnnotation = AnnotationCollector::getMethodsByAnnotation(PermissionMeta::class);
        $classAnnotation = AnnotationCollector::getClassesByAnnotation(PermissionModule::class);

        foreach ($methodAnnotation as $permissionAnnotation){
            $module = $permissionAnnotation['annotation']->module;
            if(is_null($module)){
                $module = $classAnnotation[$permissionAnnotation['class']]->value;
                if(is_null($module)){
                    throw new \Exception($permissionAnnotation['class'].'-'.$permissionAnnotation['method'].' permission annotation module define error');
                }
            }
            $identity = $permissionAnnotation['class'].'#'.$permissionAnnotation['method'];
            $value = $permissionAnnotation['annotation']->value;
            $mount = $permissionAnnotation['annotation']->mount;
            if(isset($this->userLevel[$identity]) && $this->userLevel[$identity] == UserLevel::GROUP){
                $permission = [
                    'name' => $value,
                    'module' => $module,
                    'mount' => (int)$mount
                ];
                $this->metaMap[$identity] = $permission;
            }
        }
    }

    /**
     * 获取接口等级
     * @param string $identity
     */
    public function getUserLevel(string $identity){

        return $this->userLevel[$identity] ?? UserLevel::NO_AUTH;
    }

    /**
     * 获取单个Meta
     * @param string $identity
     */
    public function getMeta(string $identity){

        return $this->metaMap[$identity]??[];
    }

    public function getMetaMap():array
    {
        return $this->metaMap;
    }

}